package com.aero.cojbackenduserservice;

public class Main {
    public static void main(String[] args) {
        String input = "[\"flower\",\"flow\",\"flight\"]";
        String content = input.substring(1, input.length() - 1);

        String[] elements = content.split("\",\"");

        // 去除每个字符串的首尾双引号
        for (int i = 0; i < elements.length; i++) {
            elements[i] = elements[i].replaceFirst("^\"|\"$", "");
        }
        String s = longestCommonPrefix(elements);
        System.out.print("'" + s + "'");
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs.length == 0){
            return "";
        }
        int minLength = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].length() < minLength){
                minLength = strs[i].length();
            }
        }
        if (minLength == 0){
            return "";
        }
        int check = check(strs, minLength);
        String substring = strs[0].substring(0, check);
        return substring;
    }

    private static int check(String[] strs, int len){
        char c = 'A';
        for (int i = 0; i < len; i++) {
            c = strs[0].toCharArray()[i];
            char a = c;
            for (int j = 0; j < strs.length; j++) {
                c = strs[j].toCharArray()[i];
                if (a != c){
                    return i;
                }
            }
        }
        return len;
    }
}